
package privatemessanger;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/**
 * This class contains the information of a message.
 * @author  Erez Fatal, Oren Slor, Elad Yanai, Sigal Agami, Chen Ben Shabat,
 *          Inbar Pardo, Islamm Heralla, Ziv Hadary, Oren Shalev, Ortal David.
 */
public class Message
{
    private String _message; // the message.
    private int _senderID; // the user ID of the sender.
    private String _senderName; // the name of the sender.
    private int _receiverID; // the user ID of the receiver.
    private String _receiverName; // the name of the receiver.
    private String _time; // the time of the message.

    /**
     * This is the Message constructor.
     * @param message The message's content.
     * @param senderID The sender's ID.
     * @param senderName The sender's name.
     * @param receiverID The receiver's ID.
     * @param receiverName The receiver's name.
     * @param time The time.
     */
    public Message(String message, int senderID, String senderName, int receiverID, String receiverName, String time)
    {
        _message=message;
        _senderID=senderID;
        _senderName=senderName;
        _receiverID=receiverID;
        _receiverName=receiverName;
        _time=time;
    }

    /**
     * Get the message's content.
     * @return The content.
     */
    public String getMessage() { return _message; }

    /**
     * Get the size of message's content.
     * @return The size.
     */
    public int getSize() { return _message.length(); }

    /**
     * Get the sender's ID.
     * @return The sender's ID.
     */
    public int getSenderID() { return _senderID; }

    /**
     * Get the sender's name.
     * @return The sender's name.
     */
    public String getSenderName() { return _senderName; }

    /**
     * Get the receiver's ID.
     * @return The receiver's ID.
     */
    public int getReceiverID() { return _receiverID; }

    /**
     * Get the receiver's name.
     * @return The receiver's name.
     */
    public String getReceiverName() { return _receiverName; }

    /**
     * Get the message's Time.
     * @return The time.
     */
    public String getTime() { return _time; }

    /**
     * Get the protocol of a message.
     * @return The protocol.
     */
    public String getProtocol() { return "MESSAGE "+_senderID+" "+_senderName+" "+_receiverID+" "+_receiverName+" "+_time; }

    /**
     * This method create a new massage.
     * @param protocol The message's details.
     * @param message The message's content.
     * @return The massage.
     */
    public static Message Create(String protocol, String message)
    {
        String[] words=protocol.split(" ");
        int senderID=Integer.decode(words[0]);
        int receiverID=Integer.decode(words[2]);
        Message msg = new Message(message,senderID, words[1], receiverID, words[3], words[4]);
        MainFrame.getLog().getMsg(msg);
        MainFrame.getLog().closeLog();
        if (!MainFrame._isMute)
        {
            try {
                InputStream in = new FileInputStream("waterdrip.wav");
                AudioStream as = new AudioStream(in);
                AudioPlayer.player.start(as);
            } catch (IOException ex) { Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex); }
        }
        return new Message(message,senderID, words[1], receiverID, words[3], words[4]);
    }
}
